//
//  MYMyCommentBaseListVC.m
//  MuYing
//
//  Created by tbb on 2019/8/9.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMyCommentBaseListVC.h"
#import "MYMyCommentListReplyCell.h"
#import "YQPCommentListModel.h"
#import "MYDynamicCommentReplyVC.h"
@interface MYMyCommentBaseListVC ()

@end

@implementation MYMyCommentBaseListVC


- (void)viewDidLoad {
    [super viewDidLoad];

}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColorA(0x000000, 0.1);
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea-44);
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMyCommentListUserCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMyCommentListContentCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYMyCommentListReplyCell")
    [self setupRefresh:self.listTableView];
    [self onStartRefresh];
}
- (void)onStartRefresh {
    self.page = 1;
    [self getCommentList];
}
-(void)onStartLoadMore {
    self.page ++ ;
    [self getCommentList];
}

-(void)getCommentList {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"limit"] = @"10";
    parameters[@"page"] = @(self.page);
    NSString *url = kPOST_USER_MY_SEND_COMMENT_URL;
    if (self.type == 1) {
        url = kPOST_URSER_MY_RECEIVE_COMMENT_URL;
    }
    YQSHOWLOADINGTIPS(@"")
    [YQPNetWorkingManager sendPostRequest:url parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            NSArray *arr = [YQPCommentModel mj_objectArrayWithKeyValuesArray:responseObject.data[@"list"]];
            [self.dataArr addObjectsFromArray:arr];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        [self stopRefresh];
        YQHIDELOADINGTIPS
    }];
}
#pragma mark - JXCategoryListContentViewDelegate

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.listTableView;
}
#pragma mark - JXCategoryListContentViewDelegate end

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YQPCommentModel *commentModel = nil;
    if (section < self.dataArr.count) {
        commentModel = self.dataArr[section];
    }
    if (commentModel.parentContent) {
        return 3;
    }
    return 2;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPCommentModel *commentModel = nil;
    if (indexPath.section < self.dataArr.count) {
        commentModel = self.dataArr[indexPath.section];
    }
    if (indexPath.row == 0) {
        MYMyCommentListUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMyCommentListUserCell"];
        cell.commentBtn.hidden = (self.type == 0 ? YES : NO);
        cell.commentModel = commentModel;
        return cell;
    }else if (indexPath.row == 1) {
        MYMyCommentListContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"MYMyCommentListContentCell"];
        contentCell.commentLb.text = commentModel.content;
        return contentCell;
    }else{
        MYMyCommentListReplyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMyCommentListReplyCell"];
        
        cell.commentModel = commentModel;
        
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPCommentModel *commentModel = nil;
    if (indexPath.section < self.dataArr.count) {
        commentModel = self.dataArr[indexPath.section];
    }
    if (indexPath.row == 0) {
        return 58;
    }else if (indexPath.row == 1) {
        return [tableView fd_heightForCellWithIdentifier:@"MYMyCommentListContentCell" configuration:^(MYMyCommentListContentCell *contentCell) {
                contentCell.commentLb.text = commentModel.content;
        }];
    }else{
        return [tableView fd_heightForCellWithIdentifier:@"MYMyCommentListReplyCell" configuration:^(MYMyCommentListReplyCell *cell) {
                cell.commentModel = commentModel;
        }];
    }
//    return 94;
}
//评论类型：type 1->帖子评论；2->资讯评论；3->商品评论；4->酒店评论；5->景点评论；6->课程评论
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPCommentModel *commentModel = nil;
    if (indexPath.section < self.dataArr.count) {
        commentModel = self.dataArr[indexPath.section];
    }
//    if (indexPath.row == 1) {
//        if (commentModel.type == 1) {
//            MYDynamicCommentReplyVC *commentReplyVC = [MYDynamicCommentReplyVC new];
//            commentReplyVC.getModel = commentModel;
//            kYQPUSH(commentReplyVC);
//        }
//    }else if(indexPath.row == 2){
//        if (commentModel.type == 1) {
//            MYDynamicCommentReplyVC *commentReplyVC = [MYDynamicCommentReplyVC new];
////            commentReplyVC.getModel = commentModel;
//            kYQPUSH(commentReplyVC);
//        }
//    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

@end
